package com.miku.utils;

import java.math.BigInteger;

public class SQLHelper {
	public static int intVal(Object o){
		if(o == null) return 0;
		
		if(o instanceof BigInteger)
			return ((BigInteger)o).intValue();
		
		if(o instanceof Integer || o.getClass().equals(int.class))
			return (Integer)o;
		else if(o instanceof Float || o.getClass().equals(float.class))
			return (int)(float)(Float)o;
		else if(o instanceof Double || o.getClass().equals(double.class))
			return (int)(double)(Double)o;
		else if(o instanceof String){
			String v = o.toString();
			if(v.equals("")) return 0;
			return Integer.parseInt(o.toString());
		}
		
		return (Integer)o;
	}
	
	public static long longVal(Object o){
		if(o == null) return 0;
		
		if(o instanceof BigInteger)
			return ((BigInteger)o).longValue();
		
		if(o instanceof Long || o.getClass().equals(long.class))
			return (Long)o;
		else if(o instanceof Integer || o.getClass().equals(int.class))
			return (Integer)o;
		else if(o instanceof Float || o.getClass().equals(float.class))
			return (long)(float)(Float)o;
		else if(o instanceof Double || o.getClass().equals(double.class))
			return (long)(double)(Double)o;
		else if(o instanceof String){
			String v = o.toString();
			if(v.equals("")) return 0;
			return Long.parseLong(o.toString());
		}
		
		return (Long)o;
	}
	
	public static String countSQL(String sql){
		return "select count(1) from (" + sql + ") temp_count";
	}
}
